package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.WelcomeViewpagerAdapter;
import com.weixin.paleituo.base.BaseActivity;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.bean.WelcomeImgsBean;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.PreferencesUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPagerHead;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

import static com.weixin.paleituo.activity.FlashActivity.SP_WELCOME_TAG;

/**
 * 首次安装，页面引导
 *
 * @author fengzhen
 * @version v1.0, 2018/4/14
 */
public class WelcomeActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    @BindView(R.id.viewpager_activity_welcome)
    ViewPager mViewpagerActivityWelcome;
    @BindView(R.id.btn_taste_to_activity_welcome)
    TextView mBtnTasteToActivityWelcome;
    private LoadingPagerHead mLoadingPagerHead;

    private List<WelcomeImgsBean.ResultBean> mWelcomeList = new ArrayList<>();
    private List<View> mViewList = new ArrayList<>();
    private WelcomeViewpagerAdapter mWelcomeAdapter;

    public static Intent call2WelcomeActivity(Context context) {
        return new Intent(context, WelcomeActivity.class);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_welcome);
    }

    @Override
    protected void initData() {
        super.initData();

        mViewpagerActivityWelcome.addOnPageChangeListener(this);
        getImgs();
    }

    private void getImgs() {
        Observable<StatusCode<WelcomeImgsBean>> observable =
                ApiUtils.getApi().getWelcomeImgs("3", Contans.defaultToken);
        HttpUtil.getInstance().toSubscribe(observable,
                new ProgressSubscriber<WelcomeImgsBean>(this) {
                    @Override
                    protected void _onNext(StatusCode<WelcomeImgsBean> statusCode) {
                        mWelcomeList.clear();
                        mWelcomeList.addAll(statusCode.getData().getResult());

                        for (WelcomeImgsBean.ResultBean bean :
                                mWelcomeList) {
                            ImageView imageView = (ImageView) UIUtils.inflate(WelcomeActivity.this, R.layout.view_welcome, mViewpagerActivityWelcome, false);
                            Glide.with(WelcomeActivity.this)
                                    .load(bean.getImg())
                                    .asBitmap()
                                    .placeholder(R.drawable.shushe_launch_screens)
                                    .into(imageView);
                            mViewList.add(imageView);
                        }

                        mWelcomeAdapter = new WelcomeViewpagerAdapter(mViewList);
                        mViewpagerActivityWelcome.setAdapter(mWelcomeAdapter);
                    }

                    @Override
                    protected void _onError(String message) {

                        LoginActivity.startActivity(WelcomeActivity.this,false);
                         finish();

                    }
                }, "", lifecycleSubject, true, true);
    }

    @OnClick(R.id.btn_taste_to_activity_welcome)
    public void onClickM() {
//        MainActivity.startActivity(WelcomeActivity.this);
        PreferencesUtils.getInstance().putBoolean(SP_WELCOME_TAG, false);
        LoginActivity.startActivity(WelcomeActivity.this, false);
        finish();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        if (position == mWelcomeList.size() - 1) {
            mBtnTasteToActivityWelcome.setVisibility(View.VISIBLE);
        } else {
            mBtnTasteToActivityWelcome.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
}
